package com.rockcent.service.impl;

import com.rockcent.common.common.PageResult;
import com.rockcent.helper.MallMemberHelper;
import com.rockcent.mall.lib.domain.MallMember;
import com.rockcent.mall.lib.domain.MallMemberFundFlow;
import com.rockcent.mall.lib.domain.MallMemberRechargeFlow;
import com.rockcent.mall.lib.domain.RockcentFund;
import com.rockcent.mall.lib.domain.enumclass.MallMemberFundTransactionType;
import com.rockcent.mall.lib.domain.enumclass.MallMemberRechargeType;
import com.rockcent.mall.lib.dto.MallMemberRechargeFlowDto;
import com.rockcent.mall.lib.repository.MallMemberFundFlowRepository;
import com.rockcent.mall.lib.repository.MallMemberRechargeFlowRepository;
import com.rockcent.mall.lib.repository.MallMemberRepository;
import com.rockcent.mall.lib.repository.RockcentFundRepository;
import com.rockcent.service.MallMemberFundFlowSrv;
import com.rockcent.service.MallMemberRechargeSrv;
import com.rockcent.service.MallMemberSrv;
import com.rockcent.service.RockcentFundSrv;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by zhangjh on 2017/3/8.
 */
@Service
public class MallMemberRechargeSrvImpl implements MallMemberRechargeSrv {
    private final Logger log = LoggerFactory.getLogger(MallMemberRechargeSrvImpl.class);


    @Autowired
    private MallMemberRechargeFlowRepository mallMemberRechargeFlowRepository;

    @Autowired
    private MallMemberFundFlowSrv mallMemberFundFlowSrv;


    @Autowired
    private MallMemberSrv mallMemberSrv;


    @Autowired
    private RockcentFundSrv rockcentFundSrv;

    @Override
    public MallMemberRechargeFlow findTop1ByMallIdOrderByLastUpdatedDesc(Long mallId) {
        return null;
    }

    @Override
    public void save(MallMemberRechargeFlow dto) {
        mallMemberRechargeFlowRepository.save(dto);
    }

    @Override
    public PageResult<MallMemberRechargeFlowDto> findMallMemberRechargePage(MallMemberRechargeFlowDto dto, PageResult page) {
        return mallMemberRechargeFlowRepository.findMallMemberRechargePage(dto, page);
    }

    @Override
    public MallMemberFundFlow rechargeSingle(MallMember mallMember, String transactionType, BigDecimal rechargeAmount) {

        String seqNo = MallMemberHelper.SINGLETON.createRechargeSeqNo(MallMemberRechargeType.valueOf(transactionType));


        //增加充值流水

        MallMemberFundFlow fundFlow = new MallMemberFundFlow();
        fundFlow.setMallId(mallMember.getMallId());
        fundFlow.setMallMemberId(mallMember.getId());
        fundFlow.setSeqNo(seqNo);
        fundFlow.setBalance(mallMember.getBalance().add(rechargeAmount));
        fundFlow.setOperationAmount(rechargeAmount);
        fundFlow.setRemark(MallMemberFundTransactionType.valueOf(transactionType).getValue());
        fundFlow.setTransactionType(MallMemberFundTransactionType.valueOf(transactionType));


        //增加平台余额
        mallMember.setBalance(mallMember.getBalance().add(rechargeAmount));

        //增加rockcent_fund资金
        RockcentFund rockcentFund = rockcentFundSrv.findOne(1l);
        rockcentFund.setTotalAmount(rockcentFund.getTotalAmount().add(rechargeAmount));
        rockcentFund.setPresentRechargeAmount(rockcentFund.getPresentRechargeAmount().add(rechargeAmount));

        MallMemberFundFlow mallMemberRechargeFlow = mallMemberFundFlowSrv.save(fundFlow);
        mallMemberSrv.save(mallMember);
        rockcentFundSrv.save(rockcentFund);
        return mallMemberRechargeFlow;
    }


}
